package com.gm.wj.service;

import com.gm.wj.config.RedisConfig;
import com.gm.wj.dao.SysBannerDAO;
import com.gm.wj.entity.SysBanner;
import com.gm.wj.redis.RedisService;
import com.gm.wj.util.MyPage;
import com.gm.wj.util.MyTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
import java.util.Set;

/**
 * @program: wj
 * @description: 广告管理
 * @author: Huang_shengjun
 * @create: 2020-08-21 01:01
 **/
@Slf4j
@Service
public class SysBannerService {
    @Autowired
    SysBannerDAO sysBannerDAO;
    @Autowired
    RedisService redisService;

    public MyPage list(int page, int size) {
        MyPage<SysBanner> banners;
        // list缓存
        String key = "bannerpage:" + page;
        Object bannerPageCache = redisService.get(key);

        // 如果为空，就再查询一次，写入redis缓存
        if (bannerPageCache == null) {
            Sort sort = Sort.by(Sort.Direction.DESC, "id");
            Page<SysBanner> bannerInDb = sysBannerDAO.findAll(PageRequest.of(page, size, sort));
            banners = new MyPage<>(bannerInDb);
            // 设置redis过期时间  5s
            redisService.set(key, banners, RedisConfig.DATA_REDIS_KEY_TIME);
        } else {
            banners = (MyPage<SysBanner>) bannerPageCache;
        }
        return banners;
    }

    /**
     * 查询广告页启动3个的数量
     * @return
     */
    public boolean findByDisplay(){
        List a = sysBannerDAO.findByDisplay(1);
        if (a.size() == 3){
            return false;
        }else {
            return true;
        }
    }

    public SysBanner findById(int id) {
        SysBanner sysBanner;
        String key = "banner:" + id;
        Object bannerCache = redisService.get(key);

        if (bannerCache == null) {
            sysBanner = sysBannerDAO.findById(id);
            redisService.set(key, sysBanner,RedisConfig.DATA_REDIS_KEY_TIME);
        } else {
            sysBanner = (SysBanner) bannerCache;
        }
        return sysBanner;
    }

    public void addOrUpdate(SysBanner sysBanner) {
        SysBanner sysBanner1 =  findById(sysBanner.getId());

        if (sysBanner1 == null){
            sysBanner.setCreateTime( MyTime.getStringDate());
        } else {
            sysBanner.setCreateTime(sysBanner1.getCreateTime());
        }
        sysBannerDAO.save(sysBanner);

        redisService.delete("banner" + sysBanner.getId());
        Set<String> keys = redisService.getKeysByPattern("bannerpage*");
        redisService.delete(keys);
    }

    public void delete(int id) {
        sysBannerDAO.deleteById(id);

        redisService.delete("banner:" + id);
        Set<String> keys = redisService.getKeysByPattern("bannerpage*");
        redisService.delete(keys);
    }

    public void updateBannerDisplay(SysBanner sysBanner) {
        SysBanner sysBanner1 = sysBannerDAO.findById(sysBanner.getId());
        sysBanner1.setDisplay(sysBanner.isDisplay());
        sysBannerDAO.save(sysBanner1);
    }


    public List<SysBanner> listByQueryKey(String queryKey1, String queryKey2, String queryKey3) {

        // 多条件模糊查询
        queryKey1 = (queryKey1 == null && queryKey1 == "" ) ? null : "" + queryKey1 + ""; // booble值  直接用等于  不用like了
        queryKey2 = (queryKey2 == null) ? null : "%" + queryKey2 + "%";
        queryKey3 = (queryKey3 == null) ? null : "%" + queryKey3 + "%";
        List<SysBanner> sysBanners = sysBannerDAO.find(queryKey1, queryKey2, queryKey3);

        return sysBanners;

    }

    public List<SysBanner> findBannerByDisplay() {
        List<SysBanner> sysBanners = sysBannerDAO.findBannerByDisplay();
        return sysBanners;
    }

}
